import {
  SET_MOBILE,
  SET_TOKEN,
  SET_YHDTOKEN,
  SET_ROLE,
  SET_OPENID,
  SET_USERID,
  SET_RECOMMENDCODE,
  SET_REFERRALUSERID
} from "@/utils/constant/mutation-types.js";
const user = {
  namespaced: true,
  state: {
    mobile: "",
    token: "",
    Yhdtoken: "",  // 微信公众号token
    role: "",
    openid: "", // 微信公众号 openid
    userId: "",
    recommendCode: "",
    ReferralUserid: "" // 推荐用户id
  },
  getters: {
    mobile: state => state.mobile,
    token: state => state.token,
    Yhdtoken: state => state.Yhdtoken,
    role: state => state.role,
    openid: state => state.openid,
    userId: state => state.userId,
    recommendCode: state => state.recommendCode,
    ReferralUserid: state => state.ReferralUserid,
  },
  mutations: {
    [SET_MOBILE](state, mobile) {
      state.mobile = mobile;
    },
    [SET_TOKEN](state, token) {
      state.token = token;
    },
    [SET_YHDTOKEN](state, Yhdtoken) {
      state.Yhdtoken = Yhdtoken;
    },
    [SET_ROLE](state, role) {
      state.role = role;
    },
    [SET_OPENID](state, openid) {
      state.openid = openid;
    },
    [SET_USERID](state, userId) {
      state.userId = userId;
    },
    [SET_RECOMMENDCODE](state, recommendCode) {
      state.recommendCode = recommendCode;
    },
    [SET_REFERRALUSERID](state, ReferralUserid) {
      state.ReferralUserid = ReferralUserid
    }
  },
  actions: {
    setMobile({ commit }, mobile) {
      commit(SET_MOBILE, mobile);
    },
    setToken({ commit }, token) {
      commit(SET_TOKEN, token);
    },
    setYhdToken({ commit }, Yhdtoken) {
      commit(SET_YHDTOKEN, Yhdtoken);
    },
    setRole({ commit }, role) {
      commit(SET_ROLE, role);
    },
    setOpenid({ commit }, openid) {
      commit(SET_OPENID, openid);
    },
    setUserId({ commit }, userId) {
      commit(SET_USERID, userId);
    },
    setRecommendCode({ commit }, recommendCode) {
      commit(SET_RECOMMENDCODE, recommendCode);
    },
    setReferralUserid({ commit }, ReferralUserid) {
      commit(SET_REFERRALUSERID, ReferralUserid);
    },
    /* 登录 */
    login(
      { dispatch },
      { mobile, Yhdtoken, role, recommendCode, send, userId, ReferralUserid }
    ) {
      dispatch("setMobile", mobile);
      dispatch("setYhdToken", Yhdtoken);
      dispatch("setRole", role);
      dispatch("setRecommendCode", recommendCode);
      dispatch("setUserId", userId);
      dispatch("setReferralUserid", ReferralUserid);
      dispatch("app/setSend", send, { root: true });
    }
  }
};
export default user;
